<?php

/**
 * 自定义表单
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: DiyController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Home\Controller;
class DiyController extends CommonController{
    private $model;
    private $fieldname = 'diyform';
    public function _initialize() {
        parent::_initialize();
        
        $diyid = I('diyid', 0, "intval");
        if(empty($diyid)){
            $this->error("非法操作!", "javascript:;");
        }
        $this->model = D("DiyForm");
        $this->model->diyform($diyid);
        //var_dump($this->model);
        //echo $this->model->name;die;
        $this->assign("name", $this->model->name);
        $this->assign("diyid", $this->model->diyid);
    }
    
    public function index() {
        $public = $this->model->public;
        if(empty($this->model->public)){
            $this->error('后台关闭前台浏览', 'javascript:;');
            exit();
        }
        if($public == 2){
            $where = array();
        }else{
            $where = array("ifcheck" => 1);
        }
        $result = $this->model->getAll($where);
        //var_dump($result, $this->model->field_infos);die;
        $this->assign($result);
        $this->assign("fieldlist", $this->model->field_infos);
        $this->display();
    }
    
    public function post() {
        $do = I("post.do", 0);
        if($do == 0){
            $postform = $this->model->getForm($this->fieldname);
            $this->assign('postform', $postform);
            $this->display();
        }else if($do == 2){
            $postData = I("post.");
            $postData = $postData[$this->fieldname];
            $yd_fieldshash = I("post.yd_fieldshash", "", "trim");
            $yd_fields = I("post.yd_fields", "", "trim");

            if(!empty($yd_fields))
            {
                if($yd_fieldshash != md5($yd_fields. C("COOKIE_ENCODE")))
                {
                    $this->error("数据校验不对，程序返回！");
                    exit();
                }
            }
            if(!empty($yd_fields)){
                $addonfields = explode(';', $yd_fields);
                if(is_array($addonfields))
                {
                    foreach($addonfields as $v)
                    {
                        if($v==''){
                            continue;
                        }
                        $vs = explode(',', $v);
                        if(!isset($postData[$vs[0]]))
                        {
                            $postData[$vs[0]] = '';
                        }
                        else if($vs[1]=='htmltext') //HTML文本特殊处理
                        {
                            $postData[$vs[0]] = AnalyseHtmlBody($postData[$vs[0]], null, $vs[1]);
                        }else{
                            if(!isset($postData[$vs[0]])){
                                $postData[$vs[0]] = '';
                            } 
                            $postData[$vs[0]] = GetFieldValueA($postData[$vs[0]], $vs[1]);
                        }
                    }
                }
            }
            $postData['subtime'] = time();
            $postData['ifcheck'] = 0;
            $postData = M($this->model->table)->create($postData);
            if(false === $postData){
                $this->error("数据提交失败，请联系管理员！");
            }
            if(false === M($this->model->table)->add($postData)){
                $this->error("数据提交失败，请联系管理员！");
            }
            $this->success("数据提交成功！", U("index", array("diyid" => $this->model->diyid)));
        }
    }
    
    public function view() {
        $id = I('get.id', 0, "intval");
        $public = $this->model->public;
        if(empty($this->model->public)){
            $this->error('后台关闭前台浏览', 'javascript:;');
            exit();
        }
        if(empty($id)){
            $this->error('非法操作！未指定id', 'javascript:;');
        }
        if($public == 2){
            $where = array();
        }else{
            $where = array("ifcheck" => 1);
        }
        $row = $this->model->getDiyById($id);
        $this->assign('row', $row);
        $this->assign("fieldlist", $this->model->field_infos);
        $this->display();
    }
}
